/****************************************************************************
@FILENAME:	codec_433.h
@FUNCTION:	
@AUTHOR:	flege.zhang
@DATE:		2020.04.12
*****************************************************************************/


#ifndef _CODEC_433__H_
#define _CODEC_433__H_


#include "app_cfg.h"


//using tccr to decode
#define VALID_433_SIGNAL_TCCR_TIMEOUT				(0xFFFFFF - 0x57E40)//16M/2,unit:0.0625us/2 = 0.125us,45ms = 0x57E40,50ms = 0x61A80,60ms = 0x75300,500ms = 0x3D0900


#define HEAD_RELAX_THRESHOLD						(500)//us
#define DATA_RELAX_THRESHOLD						(100)//us

#define IS_433_HEAD(timeH,timeL)					(((timeH > (4000 - HEAD_RELAX_THRESHOLD)) && (timeH < (4000 + HEAD_RELAX_THRESHOLD))) && \
													  ((timeL > (4000 - HEAD_RELAX_THRESHOLD)) && (timeL < (4000 + HEAD_RELAX_THRESHOLD))))
#define IS_433_BIT1(timeH,timeL)					(((timeH > (900 - DATA_RELAX_THRESHOLD)) && (timeH < (900 + DATA_RELAX_THRESHOLD))) && \
													  ((timeL > (300 - DATA_RELAX_THRESHOLD)) && (timeL < (300 + DATA_RELAX_THRESHOLD))))
#define IS_433_BIT0(timeH,timeL)					(((timeH > (300 - DATA_RELAX_THRESHOLD)) && (timeH < (300 + DATA_RELAX_THRESHOLD))) && \
													  ((timeL > (900 - DATA_RELAX_THRESHOLD)) && (timeL < (900 + DATA_RELAX_THRESHOLD))))

enum en_dec_433_state
{
	DEC_433_HEADER 	= 0x0,
	DEC_433_DATA 	= 0x1,
};

enum en_433_OPCODE
{
	OPCODE_433_FAN_SPEED_1			= 0x01,
	OPCODE_433_FAN_SPEED_2			= 0x02,
	OPCODE_433_FAN_SPEED_3			= 0x03,
	OPCODE_433_FAN_SPEED_4			= 0x04,
	OPCODE_433_FAN_SPEED_5			= 0x05,
	OPCODE_433_FAN_SPEED_6			= 0x06,
	OPCODE_433_FAN_SPEED_7			= 0x07,
	OPCODE_433_FAN_SPEED_8			= 0x08,
	OPCODE_433_FAN_SPEED_9			= 0x09,
	OPCODE_433_FAN_SPEED_10			= 0x0A,

	OPCODE_433_FAN_NORMAL_MODE		= 0x0F,

	OPCODE_433_FAN_ON				= 0x10,
	OPCODE_433_FAN_OFF				= 0x11,

	OPCODE_433_TURN_CLOCKWISE		= 0x12,
	OPCODE_433_TURN_ANTICLOCKWISE	= 0x13,

	OPCODE_433_LAMP_ON				= 0x14,
	OPCODE_433_LAMP_OFF				= 0x15,
	OPCODE_433_LAMP_ON_OFF			= 0x16,
	OPCODE_433_ALL_OFF				= 0x17,

	OPCODE_433_TIMING_30MIN			= 0x18,
	OPCODE_433_TIMING_60MIN			= 0x19,
	OPCODE_433_TIMING_120MIN		= 0x1A,
	OPCODE_433_TIMING_240MIN		= 0x1B,
	OPCODE_433_TIMING_480MIN		= 0x1C,

	OPCODE_433_BINDING				= 0x1F,
};


void decode_433_init(void);
void decode_433(uint16_t time_low,uint16_t time_high);
void decode_433_end(void);

#endif
